﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using moose.extension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace moose.irc {

    /// <summary>
    /// Base Action Class,
    /// 
    /// provides framework for structured execution of actions, 
    /// Generally, commands and responses work the same way, the difference
    /// is simply how they are invoked.
    /// 
    /// </summary>
    [CLSCompliant(true)]
    public abstract class IrcAction : object {

        //TODO: Allow action overrides to link off of the initial action, 
        // This would likely be an alternate constructor,  anyways, will see..

        /// <summary>
        /// This would be our collection of action overrides.
        /// </summary>
        protected LinkedList<IrcAction> m_llActionOverrides;

        /// <summary>
        /// Initialize IrcAction
        /// </summary>
        public IrcAction()
            : base() {
            if (m_llActionOverrides == null) { //Only create the collection, if that collection needs to be created
                m_llActionOverrides = new LinkedList<IrcAction>();
            }
            m_llActionOverrides.AddLast(this); //This becomes our default action
            build_override_path(); //Add overrides, if they exist.
            return;
        }

        /// <summary>
        /// Initialize IrcAction
        /// </summary>
        /// <param name="action_overrides">override list</param>
        public IrcAction(LinkedList<IrcAction> action_overrides)
            : this() {
                m_llActionOverrides = action_overrides;
            return;
        }

        /// <summary>
        /// build the remainder of the override path, for execution
        /// </summary>
        private void build_override_path() {
            
            Type action_type = null;
            string action_name = this.GetType().ToString();
            object[] attributes = this.GetType().GetCustomAttributes(false);
            foreach (object attribute in attributes) {
                if (attribute.GetType() == typeof(CommandAttribute)) {
                    action_type = typeof(CommandAttribute);
                    break;
                } else if (attribute.GetType() == typeof(ResponseAttribute)) {
                    action_type = typeof(ResponseAttribute);
                    break;
                }
            }
            if (action_type == null)
                return;

            
            //If we have an override for this type, create it
            Type action_override = ExtensionManager.get_action_type(action_name, action_type);
            while (action_override != null) { //And subsequently keep doing the same thing, until we dont.
                Assembly asm = action_override.Assembly;
                m_llActionOverrides.AddLast((IrcAction)asm.CreateInstance(action_override.ToString(), false, BindingFlags.Default, null, new object[] { m_llActionOverrides }, null, null));
                action_override = ExtensionManager.get_action_type(action_override.ToString(), action_type);
            }

            return;
        }

        /// <summary>
        /// Provide Irc States that are valid for this action to execute
        /// </summary>
        protected abstract IrcState valid_states();

        /// <summary>
        /// Executes Base Action
        /// </summary>
        /// <param name="client">Client to execute action on</param>
        /// <param name="message">message for action</param>
        protected void base_action(IrcClient client, IrcMessage message) {
            LinkedListNode<IrcAction> nod_this = m_llActionOverrides.Find(this);
            if (nod_this != null) { //If there is a node for us (of course there is), im not sure this check will need to be here in production
                if (nod_this != m_llActionOverrides.First) { //if this isnt the root of the action tree
                    nod_this.Previous.Value.action(client, message); //execute that action (which may, and likely will in turn execute its parent)
                }
            }
            return;
        }

        /// <summary>
        /// Actual Action
        /// </summary>
        /// <param name="client">Client to execute action on</param>
        /// <param name="message">message for action</param>
        protected abstract void action(IrcClient client, IrcMessage message);

        /// <summary>
        /// Execute the action
        /// </summary>
        /// <param name="client">Client to execute action on</param>
        /// <param name="message">message for action</param>
        public void execute(IrcClient client, IrcMessage message) {

            if ((valid_states() & client.SessionState) == client.SessionState) {
                m_llActionOverrides.Last.Value.action(client, message);
            } else {
                //TODO: Throw Exception, or quietly log
            }

            return;
        }

    }
}
